package com.dup.jxlh.admin.controller;

import com.dup.jxlh.admin.entities.auth.AuthUser;
import com.dup.jxlh.admin.entities.model.Admin;
import com.dup.jxlh.admin.entities.model.Company;
import com.dup.jxlh.admin.service.CompanyService;
import com.dup.jxlh.admin.validator.Phone;
import com.dup.jxlh.api.entities.CommonResult;
import com.dup.jxlh.api.entities.PaginationData;
import com.dup.jxlh.api.enums.ResultEnum;
import com.dup.jxlh.api.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigInteger;

@RestController
@RequestMapping("/company")
@Validated
public class CompanyController {
    @Autowired
    private CompanyService companyService;

    @PostMapping("/getSonCompanyList")
    @PreAuthorize("hasAnyAuthority('ROLE_Admin','ROLE_CompanySet_Show')")
    public CommonResult getSonCompanyList(@AuthenticationPrincipal AuthUser authUser,
                                          PaginationData paginationData) {
        return ResultUtil.result(ResultEnum.SUCCESS, companyService.getSonCompanyList(authUser.getCompanyId(),
                paginationData));
    }

    @PostMapping("/addSonCompany")
    @PreAuthorize("hasAnyAuthority('ROLE_Admin','ROLE_CompanySet_Operate')")
    public CommonResult addSonCompany(@AuthenticationPrincipal AuthUser authUser,
                                      @Validated Company company,
                                      @RequestParam("realname") String realname,
                                      @RequestParam("phone") @Phone String phone) {
        company.setPid(authUser.getCompanyId());
        Admin admin = new Admin();
        admin.setRealname(realname);
        admin.setPhone(phone);
        if (companyService.addSonCompany(company, admin)) {
            return ResultUtil.result(ResultEnum.SUCCESS);
        }
        return ResultUtil.result(ResultEnum.ERROR);
    }

    @PostMapping("/editSonCompany")
    @PreAuthorize("hasAnyAuthority('ROLE_Admin','ROLE_CompanySet_Operate')")
    public CommonResult editSonCompany(@Validated Company company,
                                       @RequestParam("realname") String realname,
                                       @RequestParam("phone") @Phone String phone) {
        company.setPid(null);
        Admin admin = new Admin();
        admin.setId(company.getAdminId());
        admin.setRealname(realname);
        admin.setPhone(phone);
        if (companyService.editSonCompany(company, admin)) {
            return ResultUtil.result(ResultEnum.SUCCESS);
        }
        return ResultUtil.result(ResultEnum.ERROR);
    }

    @PostMapping("/resetSonCompanyAdmin")
    @PreAuthorize("hasAnyAuthority('ROLE_Admin','ROLE_CompanySet_Operate')")
    public CommonResult resetSonCompanyAdmin(@RequestParam("adminId") BigInteger adminId) {
        if (companyService.resetSonCompanyAdmin(adminId)) {
            return ResultUtil.result(ResultEnum.SUCCESS);
        }
        return ResultUtil.result(ResultEnum.ERROR);
    }
}
